package com.wyx.Filiter;

import com.alibaba.fastjson.JSON;
import com.wyx.Common.Basecontext;
import com.wyx.Common.R;
import com.wyx.Pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.util.AntPathMatcher;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

//启动类必须加@ServletComponentScan
@WebFilter(filterName = "loginCheckFilter", urlPatterns = "/*")
public class loginCheckFiliter implements Filter {
    //路径匹配器
    public static final AntPathMatcher PATH_MATCHER = new AntPathMatcher();
   @Autowired
   private RedisTemplate redisTemplate;

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        HttpServletRequest request = (HttpServletRequest) servletRequest;
        HttpServletResponse response = (HttpServletResponse) servletResponse;
        //设置不需要拦截的路径
        String[] urls = {
                "/employee/login",
                "/employee/logout",
                "/backend/**",
                "/front/**",
                "/user/login",
                "/user/logout",
                "/user/sendMsg",
                "/doc.html",
                "/webjars/**",
                "/swagger-resources",
                "/v2/api-docs"
        };
//        //设置前端的路径
//        String[]fronturls={
//                "/addressBook/**",
//                "/shoppingCart/**",
//                "/order/**",
//                "/user/**"
//        };
//        //设置后端的路径
//        String[]backendurls={
//          "/category/**",
//          "/dish/**",
//          "employee/**",
//          "/setmeal/**"
//        };
//        String []fronturls={
//                "/front/**",
//                "/user/login",
//                "/user/logout",
//                "/user/sendMsg"
//        };
        boolean check = check(request.getRequestURI(), urls);
//        System.out.println(request.getRequestURI());
//        System.out.println(check);
//        System.out.println("employee:" + request.getSession().getAttribute("employee"));
        if (check) {
            //放行
            filterChain.doFilter(request, response);
        } else {
            //使用redis来判断用户是否登录
//            if (redisTemplate.opsForValue().get())
            if (request.getSession().getAttribute("employee") != null) {
                //服务端的拦截
                Long employee = (Long) request.getSession().getAttribute("employee");
                Basecontext.savecurrentid(employee);
                filterChain.doFilter(request, response);
            } else if (request.getSession().getAttribute("user") != null) {
                //对客户端的拦截
//                if(check(request.getRequestURI(),backendurls)){
//                    response.getWriter().write(JSON.toJSONString(R.error("NOTLOGIN")));
//                    return;
//                }
                User user = (User) request.getSession().getAttribute("user");
                Basecontext.savecurrentid(user.getId());
                filterChain.doFilter(request, response);
            } else {
                response.getWriter().write(JSON.toJSONString(R.error("NOTLOGIN")));
                return;
            }
        }
    }


    public boolean check(String requestURL, String[] urls) {
        for (String url : urls) {
            boolean match = PATH_MATCHER.match(url, requestURL);
            if (match) {
                return true;
            }
        }
        return false;
    }
    public boolean checkcookie(String name,HttpServletRequest request){
        Cookie[] cookies = request.getCookies();
        if (name.equals("emplouyee")){
            for (Cookie cookie : cookies) {
                if (cookie.getName().equals("employee")){
                    System.out.println("说明已经登录了");
                    return true;
                }
            }
            return false;
        }else if (name.equals("user")){
            for (Cookie cookie : cookies) {
                if (cookie.getName().equals("user")){
                    System.out.println("说明已经登录了");
                    return true;
                }
            }
            return false;
        }
        return false;
    }
}
